<?php

class GoodController extends BaseController {
    public static function getList($shop_id){
    
       try{
         echo json_encode(Goods::ListGood($shop_id));
       
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        } 
       } 
    
    
    
    
     public static function getListgoodname($good_name){
    
       try{
         echo json_encode(Goods::ListGoodname($good_name));
       
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        } 
       } 
    
    
    public static function postRegister(){
     
        try{    
            $input=Input::json()->all();

            $shop_id=InputHelper::getInput($input,'shop_id');

            $code=InputHelper::getInput($input,'code');

            $name=InputHelper::getInput($input,'name');

            $expirate=InputHelper::getInput($input,'expirate','');
           // if(!empty($expirate) && !date('Y-m-d'))
           if(!empty($expirate) && !preg_match("/^[0-9]{1,4}\-[0-9]{1,2}\-[0-9]{1,2}$/",$expirate))

            {
                return ErrorResponse::SendErrorMessage('Expirate invalid');
            }
            $import_price=InputHelper::getInput($input,'import_price','');

            $wholesale_price=InputHelper::getInput($input,'wholesale_price','');

            $retail_price=InputHelper::getInput($input,'retail_price','');

            $ware=InputHelper::getInput($input,'ware','');

            $description=InputHelper::getInput($input,'description','');

            $unit_id=InputHelper::getInput($input,'unit_id','');
            
            $category_id=InputHelper::getInput($input,'category_id','');

            
        }catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        }
        try{
            if($good_id=Goods::Insert($code,$name,$expirate,$import_price,$wholesale_price,
            $retail_price,$ware,$description,$unit_id,$category_id,$shop_id))
            {
            
                return Response::json(array('status'=>'success','good_id'=>$good_id));    
                
            }  
                       
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        }
        
        
     }
     
     
     
     public static function postEdit(){
        try{
            $input=Input::json()->all();

            $good_id=InputHelper::getInput($input,'good_id');

            $code=InputHelper::getInput($input,'code');

            $name=InputHelper::getInput($input,'name');

            $expirate=InputHelper::getInput($input,'expirate','');
           // if(!empty($expirate) && !date('Y-m-d'))
           if(!empty($expirate) && !preg_match("/^[0-9]{1,4}\-[0-9]{1,2}\-[0-9]{1,2}$/",$expirate))

            {
                return ErrorResponse::SendErrorMessage('Expirate invalid');
            }
            $import_price=InputHelper::getInput($input,'import_price','');

            $wholesale_price=InputHelper::getInput($input,'wholesale_price','');

            $retail_price=InputHelper::getInput($input,'retail_price','');

            $ware=InputHelper::getInput($input,'ware','');

            $description=InputHelper::getInput($input,'description','');

            $unit_id=InputHelper::getInput($input,'unit_id','');
            
            $category_id=InputHelper::getInput($input,'category_id','');

            
        }catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        }
        try{
            
            if(Goods::Edit($good_id,$code,$name,$expirate,$import_price,$wholesale_price,
            $retail_price,$ware,$description,$unit_id,$category_id))
            {
            
            return Response::json(array('status'=>'success'));    
                
            }
            
                       
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        }
        
    }
 
     public function postDelete(){
             $input=Input::json()->all();

            $good_id=InputHelper::getInput($input,'good_id');

            try{
                
                if( Goods::Del($good_id)){
                    return Response::json(array('status'=>'success'));
                }             
        }
        catch(Exception $e)
        {
            return ErrorResponse::SendErrorMessage($e->getMessage());
        } 
      }
  }
?>